package cn.edu.neusoft.service.car;

import lombok.Data;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class CarIORequest {
    private int pageNum = 1;
    private int pageSize = 500;
    private String searchKey = "";
    private String operatorNameLikeStr = "";
    private String carDirect = "";
    private Integer departmentId; // 使用Integer以便能够处理null值
    private String departmentName = "";
    private Integer devChnId; // 同样使用Integer
    private String ownerName = "";
    private String ownerCode = "";
    private String queryTimeBegin = "";
    private String queryTimeEnd = "";
    private String carNumLikeStr = "";

    public String toQueryString() {
        List<String> params = new ArrayList<>();

        for (Field field : this.getClass().getDeclaredFields()) {
            try {
                field.setAccessible(true);
                Object value = field.get(this);
                if (value != null) {
                    if (value instanceof String[]) {
                        String[] values = (String[]) value;
                        for (String v : values) {
                            params.add(field.getName() + "=" + v);
                        }
                    } else {
                        params.add(field.getName() + "=" + value);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return params.isEmpty() ? "" : "?" + params.stream().collect(Collectors.joining("&"));
    }
}
